package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.entity.LensDatasource;
import com.ds.lens.data.service.dao.sql.provider.LensDatasourceProvider;
import com.ds.lens.data.vo.in.v2.datasource.LensDatasourceListInVO;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-05
 */
@Repository
public interface LensDatasourceDao {

    String COLUMNS = "id, name, type, source, ds_schema, ds_sql, metric, tags, application_id, application_key, creator, created_time, mender, modified_time";


    @InsertProvider(type = LensDatasourceProvider.class, method = "insertSelective")
    int insertSelective(LensDatasource record);

    @Select({
            "select",
            COLUMNS,
            "from lens_datasource",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "lensDatasource", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.BIGINT),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER),
            @Result(column = "source", property = "source", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ds_schema", property = "dsSchema", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ds_sql", property = "dsSql", jdbcType = JdbcType.VARCHAR),
            @Result(column = "metric", property = "metric", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tags", property = "tags", jdbcType = JdbcType.VARCHAR),
            @Result(column = "application_id", property = "applicationId", jdbcType = JdbcType.BIGINT),
            @Result(column = "application_key", property = "applicationKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "creator", property = "creator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensDatasource selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensDatasourceProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensDatasource record);


    @Select({"<script>",
            "select",
            COLUMNS,
            " from lens_datasource ",
            " where 1=1 ",
            "<if test=\"applicationIds!=null and applicationIds.size >0\">",
            " and application_id in ",
            "<foreach collection='applicationIds' item='applicationId' separator=',' open='(' close=')' >",
            "#{applicationId}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    @ResultMap("lensDatasource")
    Page<LensDatasource> listDatasource(LensDatasourceListInVO listInVO);
}
